package com.wql.rabbitmq.study;

import java.io.IOException;

import com.rabbitmq.client.Channel;
import com.rabbitmq.client.QueueingConsumer;
import com.wql.rabbitmq.ChannelFactory;
import com.wql.rabbitmq.Constant;

public class TaskTestForConsumer {
	public static void main(String[] args) throws Exception {
		for (int i = 0; i < 3; i++) {
			final int index = i;
			new Thread(new Runnable() {
				@Override
				public void run() {
					try {
						consumer(index);
					} catch (IOException e) {
						e.printStackTrace();
					} catch (InterruptedException e) {
						e.printStackTrace();
					}
				}
			}).start();
		}
	}

	private static void consumer(int index) throws IOException, InterruptedException {
		Channel channel = ChannelFactory.getChannel();
		channel.queueDeclare(Constant.TASK_NAME, true, false, false, null);
		channel.basicQos(1);
		QueueingConsumer consumer = new QueueingConsumer(channel);
		channel.basicConsume(Constant.TASK_NAME, false, consumer);
		while (true) {
			QueueingConsumer.Delivery delivery = consumer.nextDelivery();
			String msg = new String(delivery.getBody());
			System.out.println(index + "---" + msg);
			channel.basicAck(delivery.getEnvelope().getDeliveryTag(), false);
		}
	}
}
